package cn.dazhou.networkdemo

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import cn.dazhou.networkdemo.databinding.ActivityIsConnectNetworkBinding

class IsConnectNetworkActivity : AppCompatActivity() {

    private val binding by lazy { ActivityIsConnectNetworkBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)


        binding.hasActiveNetwork.setOnClickListener { showToast("hasActiveNetwork = ${NetworkUtil.hasActiveNetwork()}") }
        binding.isReachable50.setOnClickListener { NetworkUtil.isServerReachable("10.238.113.50", 8088) {
            showToast("50${if (it) "可达" else "不可达"}")
        } }
        binding.isReachableBaidu.setOnClickListener { NetworkUtil.isServerReachable("baidu.com", 443) {
            showToast("百度${if (it) "可达" else "不可达"}")
        } }
    }

    private fun showToast(message: String) {
        runOnUiThread {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
        }
    }

}